//
//  BaseViewController.m
//  TableDemo
//
//  Created by 学大教育 on 2021/12/10.
//  Copyright © 2021 jutuo. All rights reserved.
//

#import "BaseViewController.h"
#import "BaseViewController+Extension.h"
//#import "AppScene.h"
@interface BaseViewController ()
{
    BOOL firstAppear;
}
@end

@implementation BaseViewController
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.view.backgroundColor = [UIColor whiteColor];
    [self addNavigationBar];
    firstAppear = YES;
    NSLog(@"%@",NSStringFromCGRect(self.view.bounds));

    
    NSString *version = [UIDevice currentDevice].systemVersion;
    if (version.doubleValue >= 8.0 && version.doubleValue < 11.0 )
    {
        // 针对 9.0 以上的iOS系统进行处理
        self.automaticallyAdjustsScrollViewInsets=NO;
    }else{
        if (@available(ios 11.0,*)) {
            UIScrollView.appearance.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            UITableView.appearance.estimatedRowHeight = 0;
            UITableView.appearance.estimatedSectionFooterHeight = 0;
            UITableView.appearance.estimatedSectionHeaderHeight = 0;
        }
    }

//    NSNotificationCenterAddObserver(kNotif_LoginSuccess, @"loginSuccessRefreshPage", nil);
    
//    [self addNoticeForKeyboard];

}

#pragma mark - notification -
- (void)loginSuccessRefreshPage
{
    
}


- (void)layoutNavigationBar
{
    [self.navigationController.navigationBar setHidden:YES];
    if (self.navigationController.viewControllers.count > 1) {

        [self leftImage:@"common_back" target:self action:@selector(backToPrevious)];
    }
    [self.view bringSubviewToFront:self.navBar];
    [self refreshViewsForNewScene];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshViewsForNewScene) name:kNotifycation_ChangeScene object:nil];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (firstAppear) {
        [self layoutNavigationBar];
        firstAppear = NO;
    }
    //针对iOS11转场动画导致的view偏移进行修复
    if ([self.navBar isHidden]) {
        //导航栏隐藏，view top = 0
        self.view.top = 0;
    }else{
//        if (self.navigationController) {
//            CGRect frame = self.view.frame;
//            frame.origin.y = kTopHeight;
//            self.view.frame = frame;
//        }
    }
}

#pragma mark - 键盘通知
- (void)addNoticeForKeyboard {
    
    //注册键盘出现的通知
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification object:nil];
    //注册键盘消失的通知
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification object:nil];
}

///键盘显示事件
- (void)keyboardWillShow:(NSNotification *)notification {
    //获取键盘高度，在不同设备上，以及中英文下是不同的
    CGFloat kbHeight = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
    
    //计算出键盘顶端到inputTextView panel底端的距离(加上自定义的缓冲距离INTERVAL_KEYBOARD)
    CGFloat offset = 0.0;
    if (self.assistantHeight > 0) {
        offset = self.assistantHeight - (self.view.frame.size.height - kbHeight);
    }
    
    // 取得键盘的动画时间，这样可以在视图上移的时候更连贯
    double duration = [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    //将视图上移计算好的偏移
    if(offset > 0) {
        [UIView animateWithDuration:duration animations:^{
            self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height);
        }];
    }
}

///键盘消失事件
- (void)keyboardWillHide:(NSNotification *)notify {
    // 键盘动画时间
    double duration = [[notify.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    //视图下沉恢复原状
    [UIView animateWithDuration:duration animations:^{
        self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    }];
}






- (void)setTitle:(NSString *)title
{
    [super setTitle:title];
    self.navBar.title = title;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - NSNotification Center

// 更新主题，刷新view， 主题可能包括：颜色，字体，图片 ...
- (void)refreshViewsForNewScene
{
//    self.navBar.backgroundColor = [AppTheme color_nav_bg];
    self.navBar.backgroundColor = [UIColor whiteColor];

}

#pragma mark - Navigation Bar


- (void)bringNavToTop
{
    [self.view bringSubviewToFront:self.navBar];
}

- (void)backToPrevious
{
    [self.navigationController popViewControllerAnimated:YES];

}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
